package com.db.stockexchange.app;

import java.io.IOException;
import java.util.Locale;
import java.util.Random;

import com.db.stockexchange.network.Client;
import com.db.stockexchange.proto.Protocol.ExchangeMessage;

public class TradingRobot implements Runnable {
	
	private String username;
	
	public static void main(String[] args) {
		
		int robotsNumber;
		
		if (args.length > 0)
			robotsNumber = Integer.parseInt(args[0]);
		else
			robotsNumber = 1;
		
		System.out.printf("starting %d robots%n%n", robotsNumber);
		
		for (int i = 0; i < robotsNumber; i++) {
			(new Thread(new TradingRobot(i))).start();
		}
	}
	
	TradingRobot(int number) {
		username = String.format("robot_%d", number);
	}
	
	@Override
	public void run() {
		
		try {
			Client client = new Client("192.168.1.107");
			
			client.write(ExchangeMessage.newBuilder()
					.setMessageType(ExchangeMessage.MessageType.LOGON)
					.setUsername(username)
					.build()
					.toByteArray()
					);
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
			
//			String[] symbols = {"MSFT", "GOOG", "ORCL"};
			String[] symbols = {"MSFT", "JAVA", "SBER"};
			Random rand = new Random();
			
			int clientOrderId = 1;
			
//			while (!Thread.interrupted()) {
//				String symbol = symbols[rand.nextInt(symbols.length)];
//				boolean buy = rand.nextFloat() > 0.5f;
//				int qty = 100 + rand.nextInt(20);
//				double price = 2000 + 3*rand.nextGaussian();
//				
//				ExchangeMessage.Builder order = ExchangeMessage.newBuilder();
//				order.setClientOrderId(clientOrderId++);
//				order.setMessageType(ExchangeMessage.MessageType.NEW_ORDER);
//				order.setOrderType(ExchangeMessage.OrderType.LIMIT);
//				if (buy)
//					order.setSide(ExchangeMessage.Side.BUY);
//				else
//					order.setSide(ExchangeMessage.Side.SELL);
//				order.setQuantity(qty);
//				order.setSymbol(symbol);
//				order.setPrice(String.format(Locale.ENGLISH, "%.3f", price));
//				
//				client.write(order.build().toByteArray());
//				
//				try {
//					Thread.sleep(500 + rand.nextInt(1000));
//				} catch (InterruptedException ignore) {}
//			}
			
			ExchangeMessage.Builder order = ExchangeMessage.newBuilder();
			order.setMessageType(ExchangeMessage.MessageType.NEW_ORDER);
			order.setOrderType(ExchangeMessage.OrderType.LIMIT);
			
			order.setQuantity(100);
			
			double price;
//			price = 100;
			for (int i = 0; i < 50_000; i++) {
				String symbol = symbols[rand.nextInt(symbols.length)];
				price = 46 + 3*rand.nextGaussian();
//				price += 0.1;
				order.setPrice(String.format(Locale.ENGLISH, "%.4f", price));
				order.setClientOrderId(clientOrderId++);
				order.setSymbol(symbol);
				order.setSide(ExchangeMessage.Side.BUY);
				client.write(order.build().toByteArray());
				
				price = 38 + 3*rand.nextGaussian();
				order.setPrice(String.format(Locale.ENGLISH, "%.4f", price));
				order.setSide(ExchangeMessage.Side.SELL);
				client.write(order.build().toByteArray());
				
//				System.out.printf("#%d price %.2f%n", i, price);
				try {
					Thread.sleep(2);
				} catch (InterruptedException ignore) {}
			}
			
			System.out.println("finished work");
			while (true);
			
			
		} catch (IOException e) {
			System.out.println("failed to connect to the server");
		}
		
	}

}
